package com.fottas.adware.portal.service.impl;

import com.fottas.adware.mapper.TmsTaskCategoryMapper;

import com.fottas.adware.mapper.TmsTaskMapper;
import com.fottas.adware.mapper.TmsTaskSourceMapper;
import com.fottas.adware.model.*;
import com.fottas.adware.portal.domain.TaskAccordionResult;
import com.fottas.adware.portal.domain.TaskCategoryVo;
import com.fottas.adware.portal.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class TaskServiceImpl implements TaskService {

    //任务的Mapper
    @Autowired
    private TmsTaskCategoryMapper taskCategoryMapper;
    @Autowired
    private TmsTaskMapper taskMapper;
    @Autowired
    private TmsTaskSourceMapper taskSourceMapper;


    @Override
    public List<TmsTaskCategory> getAllTaskCategory() {

        return taskCategoryMapper.selectByExample(null);
    }

    @Override
    public TmsTask taskDetail(Long id) {
        return taskMapper.selectByPrimaryKey(id);
    }

    @Override
    public TaskAccordionResult accordion() {
        TmsTaskCategoryExample taskCategoryExample = new TmsTaskCategoryExample();
        taskCategoryExample.createCriteria().andShowStatusEqualTo(1);
        List<TmsTaskCategory> taskCategoryList = taskCategoryMapper.selectByExample(taskCategoryExample);
        List<TaskCategoryVo> taskCategoryVos = changeTaskCatgory(taskCategoryList);

        TmsTaskSourceExample taskSourceExample = new TmsTaskSourceExample();
        taskSourceExample.createCriteria().andStateEqualTo(1);
        List<TmsTaskSource> taskSourceList = taskSourceMapper.selectByExample(taskSourceExample);
        TaskAccordionResult taskContentResult = new TaskAccordionResult();
        taskContentResult.setTaskCategoryVos(taskCategoryVos);
        taskContentResult.setTaskSourceList(taskSourceList);
        return taskContentResult;
    }

    private List<TaskCategoryVo> changeTaskCatgory(List<TmsTaskCategory> taskCategoryList){
        List<TaskCategoryVo> taskCategoryVos = new ArrayList<>();
        taskCategoryList.forEach(tc ->{
            TaskCategoryVo taskCategoryVo = new TaskCategoryVo();
            if (tc.getParendId() == 0){
                taskCategoryVo.setId(tc.getId());
                taskCategoryVo.setName(tc.getName());
                taskCategoryVo.setTaskCount(tc.getTaskCount());
                taskCategoryVos.add(taskCategoryVo);
            }
        });
        taskCategoryVos.forEach(tcv -> {
            List<TaskCategoryVo> child = getChild(tcv.getId(), taskCategoryList);
            tcv.setChild(child);
        });
        return taskCategoryVos;
    }

    private List<TaskCategoryVo> getChild(Long id, List<TmsTaskCategory> taskCategoryList){
        List<TaskCategoryVo> childList = new ArrayList<>();
        taskCategoryList.forEach(tc -> {
            if (id.equals(tc.getParendId())){
                TaskCategoryVo tv = new TaskCategoryVo();
                tv.setId(tc.getId());
                tv.setName(tc.getName());
                tv.setTaskCount(tc.getTaskCount());
                childList.add(tv);
            }
        });
        return childList;
    }
}
